<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Custodian Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page('custodian');
		$this->load->helper('text');
		$this->view_data['system_message'] = $this->_msg();
		$this->_init_pagnate_config();	
	}
	
	public function index()
	{
		
	}
	
	public function transactions()
	{
		$this->load->model('M_custodian','mc');
		$this->view_data['system_message'] = $this->_msg();
		/* pagenation */
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'search/transactions';
		$this->pagnate_config['per_page'] = 30;
		$this->pagnate_config["uri_segment"] = 3;
		$this->pagnate_config['total_rows'] = $this->mc->count_all_transactions();

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->view_data['transactions'] = $this->mc->fetch_transactions($this->pagnate_config['per_page'],$page);
        $this->view_data["links"] = $this->pagination->create_links();
	}
	
	public function transaction()
	{
		$this->load->model('M_custodian','mc');
		$this->view_data['system_message'] = $this->_msg();
		/* pagenation */
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'search/transactions';
		$this->pagnate_config['per_page'] = 30;
		$this->pagnate_config["uri_segment"] = 3;
		$this->pagnate_config['total_rows'] = $this->mc->count_all_transactions();

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->view_data['transactions'] = $this->mc->fetch_transactions($this->pagnate_config['per_page'],$page,TRUE);
        $this->view_data["links"] = $this->pagination->create_links();
	}
	
	public function new_transactions()
	{
		
		$this->load->model('M_custodian','mc');
		$this->view_data['items'] = $this->mc->get_items_with_category();
	
		if($this->input->post('new_transactions_slip'))
		{
			$data = $this->input->post();
			
			$res = $this->mc->insert_new_transaction($data);
			if($res->status == TRUE)
			{
				$this->_msg('s',$res->log,'custodian/new_transactions');
			}else{
				$this->_msg('e',$res->log,'custodian/new_transactions');
			}
			
		}

	}
	
	public function add_items()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('M_custodian','mc');
		$this->view_data['cat'] = $this->mc->get_categories_array();
		
		if($this->input->post('add_item'))
		{
			$this->form_validation->set_rules('item[item_name]','Item Name','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('item[item_desc]','Item Description','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('item[item_qty]','Item Quantity','required|trim|numeric|htmlspecialchars');
			$this->form_validation->set_rules('item[item_cat]','Item Category','required|trim|numeric|htmlspecialchars|greater_than[0]');
			$this->form_validation->set_rules('item_unit_price','Unit Price','numeric|htmlspecialchars');
			$this->form_validation->set_rules('item_purchase_price','Purchase Price','numeric|htmlspecialchars');
			$this->form_validation->set_message('greater_than','%s Cannot Be empty');
			
			if($this->form_validation->run() !== FALSE)
			{
				$data = $this->input->post();
				if($this->mc->add_item($data))
				{
					$this->_msg('s','Item Successfully Added!','custodian/items');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','An error occured while adding item');
				}
			}else{
			
				if(validation_errors() !== '')
				{
					$this->view_data['system_message'] = $this->_msg('n',validation_errors());
				}
			}
		}
	}
	
	public function items()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('M_custodian','mc');
		$this->view_data['items'] = $this->mc->get_items();
	}
	
	public function categories()
	{
		$this->load->model('M_custodian','mc');
		$this->view_data['categories'] = $this->mc->get_categories();
	}
	
	public function add_category()
	{
		$this->view_data['system_message'] = $this->_msg();
		if($this->input->post('add_category'))
		{
			$this->form_validation->set_rules('cat[cat_name]','Category Name','required|trim|htmlspecialchars|alpha_dash|is_unique[custodian_items_categories.cic_name]');
			$this->form_validation->set_rules('cat[cat_desc]','Category Description','trim|htmlspecialchars');
		
			if($this->form_validation->run() !== FALSE)
			{
				$this->load->model('M_custodian','mc');

				if($this->mc->add_category($this->input->post('cat')))
				{
					$this->_msg('s','New category has been added.','custodian/categories');
				}else{
					$this->view_data['system_message'] = $this->_msg('e','New category was not added.');
				}
			}else{
				$this->view_data['system_message'] = $this->_msg('e','Error with the entry.');
			}	
		}
	}
	
	public function view_transaction($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{	
			$t_id = $x[0];
			
			$this->load->model('M_custodian','mc');
			
			$this->view_data['transactions_details'] = $this->mc->view_transaction_details($t_id);
		}else{
			show_404();
		}
	
	}
	
	public function delete_transaction($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$t_id = $x[0];
			$this->load->model('M_custodian','mc');
			$res = $this->mc->delete_transaction($t_id);
		
			if($res)
			{
				$this->_msg('s','Successfully Deleted Transaction','custodian/transactions');
			}else{
				$this->_msg('e','Unable to Delete Transaction','custodian/transactions');
			}
		}else{
			show_404();
		}
	}
}